//请求操作提示
import {ElMessage, ElMessageBox} from "element-plus";
import {utils} from "./utils";

export const operate = function () {
	//1.删除操作
	function remove({title,data,executeFn,hint}) {
		ElMessageBox.confirm(
			`您确定要删除${data}吗？${hint?hint:''}`,
			`${title}`,
			{
				confirmButtonText: "确认",
				cancelButtonText: "取消",
				type: 'primary'
			}
		).then(()=>{
			if(executeFn){
				executeFn();
			}
		}).catch(()=>{
			ElMessage.info('操作已取消');
		})
	}
	
	//3.批量删除
	function removeBatch({title,selection,key,executeFn,hint}) {
		if(selection===null || selection.length === 0){
			ElMessage.info(`您还未选中任何数据`);
			return;
		}
		ElMessageBox.confirm(
			`你确定要删除选中行吗？${hint?hint:''}`,
			`${title}`,
			{
				confirmButtonText: "确认",
				cancelButtonText: "取消",
				type: 'primary'
			}
		).then(()=>{
			let ids = utils.getSelectionParams(selection,key);
			if(executeFn){
				executeFn(ids);
			}
		}).catch(()=>{
			ElMessage({
				message: '操作已取消',
				type: 'info'
			})
		})
	}

	function switchStatus({title,hint}) {
		return new Promise(( resolve) => {
			ElMessageBox.confirm(
				`${hint}`,
				`${title}`,
				{
					confirmButtonText: "确认",
					cancelButtonText: "取消",
					type: 'primary'
				}
			).then(()=>{
				return resolve(true);
			}).catch(()=>{
				return resolve(false);
			})
		})
	}

	function confirm({hint,title,executeFn,cancelHint}){
		ElMessageBox.confirm(
			`${hint}`,
			`${title}`,
			{
				confirmButtonText: "确认",
				cancelButtonText: "取消",
				type: 'primary'
			}
		).then(()=>{
			executeFn();
		}).catch(()=>{
			if(cancelHint) {
				ElMessage.info('操作已取消');
			}
		})
	}
	
	//简单构造出防抖大致框架
	//func 要执行的函数，wait要等待的时间
	function debounce(func, wait) {
		let timeout;
		return function () {
			// 清空定时器
			if(timeout) clearTimeout(timeout);
			timeout = setTimeout(func, wait)
		}
	}
	return {
		debounce,
		remove,
		removeBatch,
		switchStatus,
		confirm
	}
}();

